package com.cskaoyan.service.promote;

import com.cskaoyan.bean.MallCoupon;
import com.cskaoyan.bean.MallCouponUser;
import com.cskaoyan.mapper.MallCouponMapper;
import com.cskaoyan.model.bo.promote.SimpleMallCoupon;
import com.cskaoyan.model.vo.promote_ad.BaseData;
import com.cskaoyan.model.vo.promote_ad.MyListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TODO
 *
 * @author：23117
 * @date: 2021/12/1
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    MallCouponMapper mallCouponMapper;

    @Override
    public BaseData list() {

        List<MallCoupon> mallCoupons = mallCouponMapper.selectList();
        BaseData data = new BaseData(mallCoupons);

        return data;
    }

    @Override
    public MallCoupon create(SimpleMallCoupon simpleMallCoupon) {

        MallCoupon mallCoupon = new MallCoupon();

        mallCoupon.setDays(simpleMallCoupon.getDays());
        mallCoupon.setDesc(simpleMallCoupon.getDesc());
        mallCoupon.setDiscount(simpleMallCoupon.getDiscount());
        mallCoupon.setGoodsValue(simpleMallCoupon.getGoodsValue());
        mallCoupon.setMin(simpleMallCoupon.getMin());
        mallCoupon.setTag(simpleMallCoupon.getTag());
        mallCoupon.setTotal(simpleMallCoupon.getTotal());
        mallCoupon.setName(simpleMallCoupon.getName());
        mallCoupon.setGoodsType(simpleMallCoupon.getGoodsType());
        mallCoupon.setType(simpleMallCoupon.getType());
        mallCoupon.setLimit(simpleMallCoupon.getLimit());

        Integer i = mallCouponMapper.insertSimpleMallCoupon(mallCoupon);
        if (i == 1) return mallCoupon;

        return null;
    }

    @Override
    public boolean update(MallCoupon mallCoupon) {
        Integer i = mallCouponMapper.updateMallCoupon(mallCoupon);
        return i == 1;
    }

    @Override
    public boolean delete(MallCoupon mallCoupon) {
        mallCoupon.setDeleted(true);
        Integer i = mallCouponMapper.deleteMallCoupon(mallCoupon);
        return i == 1;
    }

    //微信端的list
    @Override
    public BaseData wxList(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<MallCoupon> mallCoupons = mallCouponMapper.selectList();
        PageInfo info = new PageInfo(mallCoupons);
        int total = (int) info.getTotal();
        BaseData data = new BaseData(total, mallCoupons);
        return data;
    }

    @Override
    public BaseData wxMyList(Integer page, Integer limit) {
        //分页
        PageHelper.startPage(page, limit);

        //查数据
        List<MyListVo> myListVos = mallCouponMapper.selectWxMyList();

        //分页
        PageInfo info = new PageInfo(myListVos);
        int total = (int) info.getTotal();

        //返回数据
        BaseData data = new BaseData(total, myListVos);
        data.setPage(page);
        data.setLimit(limit);
        return data;
    }

    @Override
    public int wxExchange(String code, Integer userId) {

        //第一次调用mapper层 查询market_coupon表
        List<MallCoupon> mallCoupons = mallCouponMapper.wxSelectExchange(code);
        //如果 没有查到数据 证明传入的兑换码错误
        if (mallCoupons.size() == 0) {
            return 400;
        }

        //第二次查询market_coupon_user表     走到此处证明 兑换码没错误
        int i = mallCouponMapper.wxSelectUser(mallCoupons.get(0).getId(), userId);
        //当能根据兑换码查到数据 证明兑换码在表中存在，领取过了
        if (i != 0) return 300;

        //不能查到数据 证明还没领取
        return 0;
    }

    @Override
    public BaseData selectList(Integer cartID, Integer grouponRulesId, Integer userId) {
        List<MyListVo> myListVos = mallCouponMapper.selectConectMyList(userId);
        PageInfo info = new PageInfo(myListVos);
        int total = (int) info.getTotal();
        BaseData data = new BaseData(myListVos);
        data.setTotal(total);

        return data;
    }

    @Override
    public int receive(Integer couponId, Integer userId) {

        // data=300 已领取

        //int i = mallCouponMapper.selectReceive(couponId, userId);
        MallCoupon mallCoupon = mallCouponMapper.selectByPrimaryKey(couponId);
        Short limit = mallCoupon.getLimit();
        if (limit==0){
            int a = mallCouponMapper.insertOne(couponId, userId);
            return 0;
        }
        int i = mallCouponMapper.selectReceive(couponId, userId);

        if (i != 0) return 300;
        else {
            //要写一条sql语句
            int a = mallCouponMapper.insertOne(couponId, userId);

            return 0;
        }
    }

    @Override
    public MallCoupon read(Integer id) {
        List<MallCoupon> mallCoupons = mallCouponMapper.read(id);
        MallCoupon mallCoupon = mallCoupons.get(0);
        return mallCoupon;
    }

    @Override
    public BaseData listUser(Integer page, Integer limit, Integer couponId) {
        PageHelper.startPage(page, limit);

        List<MallCouponUser> mallCouponUsers = mallCouponMapper.listUser(couponId);


        PageInfo info = new PageInfo(mallCouponUsers);
        int total = (int) info.getTotal();

        BaseData data = new BaseData(total, mallCouponUsers);


        return data;
    }
}
